﻿using DevExpress.XtraBars;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraPrinting;
using DevTeam;
using System;
using System.Data;
namespace ConfExpert.Forms.ImportWizards
{
    public partial class GP_7C_Tip1 : XImportWizard
    {
        public GP_7C_Tip1()
        {
            InitializeComponent();

            Init("7C Barkod Dosyası - Dosya Bazlı", false);
            this.FileDone += GP_7C_Tip1_FileDone;
            this.ProcessDone += GP_7C_Tip1_ProcessDone;
            btnSave.Enabled = false;
            pnlFilter.Visible = false;
            btnGP.Visibility = BarItemVisibility.Never;
        }

        void GP_7C_Tip1_ProcessDone()
        {
            btnSave.Enabled = false;
        }

        bool GP = false;
        DataTable dtResult = null;
        void GP_7C_Tip1_FileDone(DataSet ds, string FileName)
        {
            dtResult = new DataTable("Table");

            GP = (FileName == "GP");    //*
            if (GP)
            {
                string[] colNames = { "Modello", "Tessuto" };

                foreach (string colName in colNames)
                    dtResult.Columns.Add(colName, typeof(String));

                foreach (DataTable dt in ds.Tables)
                    for (int i = 1; i < dt.Rows.Count; i++)
                    {
                        DataRow dr = dt.Rows[i];
                        dtResult.Rows.Add(
                            dr[0].ToString(),
                            dr[1].ToString()
                            );
                    }
            }
            else
            {
                string[] colNames = { "Barcode", "Modello", "Tessuto", "Taglia", "Descrizione", "Prezzo", "NumeroDocumento", "Sezionale", "Desc1", "Desc2", "Tg2", "Europa" };

                foreach (string colName in colNames)
                    dtResult.Columns.Add(colName, typeof(String));

                dtResult.AddAutoIncrementalColumn("BarkodGPId");
                dtResult.SetPK("BarkodGPId"); 
                
                foreach (DataTable dt in ds.Tables)
                    for (int i = 1; i < dt.Rows.Count; i++)
                    {
                        DataRow dr = dt.Rows[i];
                        dtResult.Rows.Add(
                            dr[0].ToString(),
                            dr[1].ToString(),
                            dr[2].ToString(),
                            dr[4].ToString(),
                            dr[5].ToString(),
                            dr[6].ToString(),
                            dr[7].ToString(),
                            dr[8].ToString(),
                            dr[9].ToString(),
                            dr[10].ToString(),
                            dr[11].ToString(),
                            dr[12].ToString()
                            );
                    }
            }




            DataTable dtModel = dtResult.DefaultView.ToTable(true, "Modello");
            foreach (DataRow dr in dtModel.Select("Modello is null"))            
                dtModel.Rows.Remove(dr);            

            dtResult.DefaultView.Sort = "Modello";

            cmbModel.Properties.DataSource = dtModel.DefaultView.ToTable();
            cmbModel.Properties.DisplayMember = "Modello";
            cmbModel.Properties.ValueMember = "Modello";

            cmbModel.EditValueChanged += cmbModel_EditValueChanged;
            cmbModel.EditValue = dtModel.Rows[0]["Modello"];


            /*
            MasterGrid.Init(dtResult);
            MasterGrid.Update("BarkodGPId, Barcode, Modello, Tessuto, Taglia, Descrizione, Prezzo, NumeroDocumento, Sezionale, Desc1, Desc2, Tg2, Europa", "BarkodGP");
            */


            btnSave.Enabled = false;
            pnlFilter.Visible = true;
        }

        void cmbModel_EditValueChanged(object sender, EventArgs e)
        {
            DataTable dtArtikel = (dtResult.Select("Modello='" + cmbModel.EditValue.ToString() + "'")).CopyToDataTable().DefaultView.ToTable(true, "Tessuto");
            dtArtikel.DefaultView.Sort = "Tessuto";
            cmbArticle.Properties.DataSource = dtArtikel.DefaultView.ToTable();
            cmbArticle.Properties.DisplayMember = "Tessuto";
            cmbArticle.Properties.ValueMember = "Tessuto";
        }

        private void btnReport_Click(object sender, EventArgs e)
        {
            string modello = cmbModel.EditValue.ToString();
            string tessuto = cmbArticle.Properties.GetCheckedItems().ToString();
            if (tessuto == "")
            {
                ShowErrorMessage("Tessuto seçin!");
                return;
            }

            string filter = "Modello = '" + modello + "' and Tessuto in (";

            foreach (string item in tessuto.Split(','))
                filter += "'" + item.Trim() + "',";

            filter = filter.RemoveIfEndsWith(",");
            filter += ")";

            DataTable dtGrid = dtResult.Select(filter).CopyToDataTable();

            dtGrid = ExecuteDataTable("GetGPSiparis" + (GP ? "_Direct" : string.Empty), dtGrid.Serialize());

            MasterGrid.Init(dtGrid);
            MasterGrid.MakeReadOnly();
            MasterGrid.BestFitColumns();
        }

        private void GP_7C_Tip1_Load(object sender, EventArgs e)
        {
            btnGP.ItemClick += BtnGP_ItemClick;
            btnExcel.Click += btnExcel_Click;
        }

        void btnExcel_Click(object sender, EventArgs e)
        {
            btnReport.PerformClick();
            string fileName = Dialogs.SaveFile("Excel Document", "Excel Documents|*.xlsx");

            if (fileName != string.Empty)
            {
                XlsxExportOptions opt = new XlsxExportOptions();
                opt.TextExportMode = TextExportMode.Value;

                bool org = MasterGrid.OptionsView.AllowCellMerge;
                MasterGrid.OptionsView.AllowCellMerge = false; 

                foreach (GridColumn gc in MasterGrid.Columns)
                    if (gc.ColumnEdit != null)
                        switch (gc.ColumnEdit.GetType().ToString())
                        {
                            case "RepositoryItemLookUpEdit":
                            case "RepositoryItemGridLookUpEdit":
                            case "RepositoryItemTimeSpanEdit":
                                ((RepositoryItem)gc.ColumnEdit).ExportMode = ExportMode.DisplayText;
                                break;
                        }

                MasterGrid.OptionsView.ColumnAutoWidth = false;
                MasterGrid.OptionsPrint.AutoWidth = false;
                MasterGrid.BestFitColumns();

                MasterGrid.ExportToXlsx(fileName, opt);
                MasterGrid.OptionsView.AllowCellMerge = org;
            }
        }

        private void BtnGP_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            GP_7C_Tip1_FileDone(ExecuteDataSet("GetGPSiparis_Direct", string.Empty), "GP");    //*
        }
    }
}
